package com.example.test;

import java.io.IOException;
import java.util.List;

import com.example.test.fragment.BindFragment;
import com.example.test.fragment.IndexFragmet;
import com.example.test.fragment.UserFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.AvoidXfermode.Mode;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends SlidingFragmentActivity implements OnClickListener {


	@ViewInject(R.id.imgbtn_left)
	private ImageView imgbtn_left;
	@ViewInject(R.id.rl_tiltebar_left)
	private  RelativeLayout rl_left;
	@ViewInject(R.id.rl_tiltebar_right)
	private RelativeLayout rl_right;
	@ViewInject(R.id.imgbtn_right)
	private ImageView imagebtn_right;
	@ViewInject(R.id.tv_username)
	private TextView user_name;
	@ViewInject(R.id.txt_title)
	private TextView txt_title;	
	@ViewInject(R.id.ly_index)
	private LinearLayout ly_index;
	@ViewInject(R.id.ly_myuser)
	private LinearLayout ly_myuser;
	@ViewInject(R.id.ly_incrase)
	private LinearLayout ly_incrase;
	@ViewInject(R.id.ly_exit)
	private LinearLayout ly_exit;
	@ViewInject(R.id.ly_changeuser)
	private LinearLayout ly_changeuser;
	private SharedPreferences sp;
	private Fragment fragment;
	private String username;
	private SlidingMenu sm;
	private View view;
	private TextView title;
	private TextView message;
	private Button ok;
	private Button cancel;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.activity_main);
		setBehindContentView(R.layout.left);
		sm=getSlidingMenu();
		sm.setMode(SlidingMenu.LEFT);
		
		// 3.设置滑动菜单出来后，内容页的剩余宽度
		sm.setBehindWidthRes(R.dimen.behind_layout);
		// 4.设置阴影图片
		//sm.setShadowDrawable(R.drawable.drawer_shadow);
		// 5.设置阴影图片宽度
		//sm.setShadowWidthRes(R.dimen.shadwith);
		// 6.设置滑动菜单的范围
		// 第一个参数SlidingMenu.TOUCHMODE_FULLSCREEN：可用全屏滑动
		// 第二个参数SlidingMenu.TOUCHMODE_MARGIN:只能在边缘滑动
		// SlidingMenu.TOUCHMODE_NONE不能滑动
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		ViewUtils.inject(this);
		rl_left.setOnClickListener(this);				
		initData();

	}

	private void changerUserAlter() {
		final Dialog dialog = new Dialog(this,R.style.MyDialog);
        dialog.show();
        dialog.getWindow().setGravity(Gravity.CENTER);
        dialog.getWindow().setLayout(

                LayoutParams.WRAP_CONTENT, 

                LayoutParams.WRAP_CONTENT);

    dialog.getWindow().setContentView(R.layout.dilog_layout);   

	title = (TextView) dialog.findViewById(R.id.tv_title);
	message = (TextView) dialog.findViewById(R.id.tv_message);
	ok=(Button) dialog.findViewById(R.id.button_ok);
	cancel=(Button) dialog.findViewById(R.id.button_cancel);
	title.setText("切换账号");
	message.setText("确认要退出么?");
	ok.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			sp = getSharedPreferences("user",
					MODE_PRIVATE);
			Editor edit = sp.edit();
			edit.putBoolean("login", false);
			edit.commit();
			Intent intent = new Intent(
					MainActivity.this,
					LoginActivity.class);
			startActivity(intent);
			MainActivity.this.finish();
		}
	});
	cancel.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dialog.dismiss();
		}
	});
	dialog.show();
	}
	
	private void exit() {
		final Dialog dialog = new Dialog(this,R.style.MyDialog);
	        dialog.show();
	        dialog.getWindow().setGravity(Gravity.CENTER);
	        dialog.getWindow().setLayout(

	                LayoutParams.WRAP_CONTENT, 

	                LayoutParams.WRAP_CONTENT);

	    dialog.getWindow().setContentView(R.layout.dilog_layout);   

		title = (TextView) dialog.findViewById(R.id.tv_title);
		message = (TextView) dialog.findViewById(R.id.tv_message);
		ok=(Button) dialog.findViewById(R.id.button_ok);
		cancel=(Button) dialog.findViewById(R.id.button_cancel);
		title.setText("退出程序");
		message.setText("确认要退出么?");
		ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MainActivity.this.finish();
			}
		});
		cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		dialog.show();
	}
	private void initData() {
		sp = getSharedPreferences("user", MODE_PRIVATE);
		username = sp.getString("username", null);
		// TODO Auto-generated method stub
		user_name.setText(username);
		ly_changeuser.setOnClickListener(this);
		ly_exit.setOnClickListener(this);
		ly_index.setOnClickListener(this);
		ly_myuser.setOnClickListener(this);
		ly_incrase.setOnClickListener(this);
		IndexFragmet fragmet = new IndexFragmet();
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.content_frame, fragmet, "index").commit();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK){
			exit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.rl_tiltebar_left:
			sm.toggle();
			break;

		case R.id.ly_index:
			txt_title.setText(R.string.index);
			fragment = getSupportFragmentManager().findFragmentByTag(
					"index");
			if (fragment == null) {
				fragment = new IndexFragmet();
			}
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_frame, fragment, "index")
					.commit();
			sm.toggle();
			break;
		case R.id.ly_changeuser:
			changerUserAlter();
			sm.toggle();
			break;
		case R.id.ly_exit:
			exit();
			sm.toggle();
			break;
		case R.id.ly_incrase:
			txt_title.setText(R.string.incrase);
			fragment = getSupportFragmentManager().findFragmentByTag(
					"incrase");
			if (fragment == null) {
				fragment = new BindFragment();
			}
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_frame, fragment, "incrase")
					.commit();
			sm.toggle();
			break;
		case R.id.ly_myuser:
			txt_title.setText(R.string.myuser);
			fragment = getSupportFragmentManager().findFragmentByTag(
					"myuser");
			if (fragment == null) {
				fragment = new UserFragment();
			}
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_frame, fragment, "myuser")
					.commit();
			sm.toggle();
			break;
		default:
			break;
		}
	}


}
